package dyyx.bloomfilter;

public class BloomFilterUtil {

	public static void main(String[] args) throws Exception {
		
		int expectedInsertions = 10000;
		double fpp = 0.001;
		fpp = 0.0001;
		long numBits = optimalNumOfBits(expectedInsertions, fpp);
		 
	    int numHashFunctions = optimalNumOfHashFunctions(expectedInsertions, numBits);
	    
	    System.out.println("numBits="+numBits);
	    System.out.println("numHashFunctions="+numHashFunctions);



	}

	public static long optimalNumOfBits(long expectedInsertions, double fpp) {
		if (fpp == 0) {
			fpp = Double.MIN_VALUE;
		}
		return (long) (-expectedInsertions * Math.log(fpp) / (Math.log(2) * Math.log(2)));
	}

	public static int optimalNumOfHashFunctions(long expectedInsertions, long numBits) {
		// (m / n) * log(2), but avoid truncation due to division!
		return Math.max(1, (int) Math.round((double) numBits / expectedInsertions * Math.log(2)));
	}

}
